package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import com.tanhua.server.exeception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/30
 */
@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private FriendApi friendApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private AnnouncementApi announcementApi;

    public UserInfoVo findUserInfoByHuanXin(String huanxinId) {
        User user = userApi.findByHuanXIn(huanxinId);
        UserInfo userInfo = userInfoApi.findByid(user.getId());
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        if (userInfo.getAge() != null) {
            userInfoVo.setAge(userInfo.getAge().toString());
        }
        return userInfoVo;
    }

    public void contacts(Long friendId) {
        Long userId = UserHolder.getUserId();
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId,
                Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.save(userId, friendId);
    }

    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        List<Friend> list = friendApi.findByUserID(UserHolder.getUserId(), page, pagesize);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        List<Long> friendIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        UserInfo userInfo=new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendIds, userInfo);

        List<ContactVo> vos =new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo1 = map.get(friend.getFriendId());
            if(userInfo1!=null){
                ContactVo init = ContactVo.init(userInfo1);
                vos.add(init);
            }
        }

        return new PageResult(page,pagesize,0l,vos);
    }

    public PageResult likes(Integer page, Integer pagesize) {
         List<Comment> list=commentApi.likes(page,pagesize,UserHolder.getUserId(),CommentType.LIKE);
         if(CollUtil.isEmpty(list)){
             return new PageResult();
         }

        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);

        List<CommentVo>  vos=new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo!=null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }

        return new PageResult(page,pagesize,0l,vos);
    }

    public PageResult comments(Integer page, Integer pagesize) {
        List<Comment> list=commentApi.likes(page,pagesize,UserHolder.getUserId(),CommentType.COMMENT);
        if(CollUtil.isEmpty(list)){
            return new PageResult();
        }

        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        List<CommentVo>  vos=new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo!=null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    public PageResult loves(Integer page, Integer pagesize) {
        List<Comment> list=commentApi.likes(page,pagesize,UserHolder.getUserId(),CommentType.LOVE);
        if(CollUtil.isEmpty(list)){
            return new PageResult();
        }

        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);

        List<CommentVo>  vos=new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo!=null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    public PageResult announcements(Integer page, Integer pagesize) {
        IPage<Announcement> list=announcementApi.announcements(page,pagesize);
        if (list==null){
            return new PageResult();
        }

        List<AnnouncementVo> vos =new ArrayList<>();
        for (Announcement announcement : list.getRecords()) {
            AnnouncementVo init = AnnouncementVo.init(announcement);
            vos.add(init);
        }

        return new PageResult(page,pagesize,list.getTotal(),vos);
    }
}
